/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import org.jbfilter.core.CleaningStrategy;
import org.jbfilter.core.EnableStrategy;
import org.jbfilter.core.fcomps.delegate.NullTestingEnableableDelegate;
import org.jbfilter.core.fcomps.delegate.SingleFilterComponentDelegate;
import org.jbfilter.core.fcomps.single.ContainsStringFilterComponent;
import org.jbfilter.core.fcomps.single.RegexFilterComponent;
import org.jbfilter.core.fcomps.single.SingleFilterComponent;
import org.jbfilter.core.fcomps.single.StringFilterComponent;

abstract class _StringFilterComponentImpl<E>
	implements
		StringFilterComponent<E> {

	private boolean regex = false;
	private Boolean regexCleanValue = false;
	
	// inner fComps
	private ContainsStringFilterComponent<E> containsFc;
	private RegexFilterComponent<E> regexFc;
	//delegates
	private SingleFilterComponentDelegate<E, String> sfcDelegate;
	private NullTestingEnableableDelegate nteDelegate;
	
	_StringFilterComponentImpl() {
		sfcDelegate = new SingleFilterComponentDelegate<E, String>();
		nteDelegate = new NullTestingEnableableDelegate();
		containsFc = initContainsFc();
		regexFc = initRegexFc();

		// register to delegate
		sfcDelegate.add(containsFc);
		sfcDelegate.add(regexFc);
		nteDelegate.add(containsFc);
	}
	protected SingleFilterComponent<E, String> getActiveFc() {
		return regex ? regexFc : containsFc;
	}
	protected ContainsStringFilterComponent<E> getContainsFc() {
		return containsFc;
	}
	protected RegexFilterComponent<E> getRegexFc() {
		return regexFc;
	}
	abstract protected ContainsStringFilterComponent<E> initContainsFc();
	abstract protected RegexFilterComponent<E> initRegexFc();

	@Override
	public String getValue() {
		return sfcDelegate.getValue();
	}
	@Override
	public void setValue(String value) {
		sfcDelegate.setValue(value);
	}
	@Override
	public String getValueCleanValue() {
		return sfcDelegate.getValueCleanValue();
	}
	@Override
	public void setValueCleanValue(String valueCleanValue) {
		sfcDelegate.setValueCleanValue(valueCleanValue);
	}
	@Override
	public void setEnabled(boolean enabled) {
		sfcDelegate.setEnabled(enabled);
	}
	@Override
	public boolean isEnabled() {
		return sfcDelegate.isEnabled();
	}
	@Override
	public boolean isActive() {
		return sfcDelegate.isActive();
	}
	@Override
	public void clean() {
		sfcDelegate.clean();
		if (regexCleanValue != null) {
			regex = regexCleanValue.booleanValue();
		}
	}
	@Override
	public void setCleaningStrategy(CleaningStrategy cleaningStrategy) {
		sfcDelegate.setCleaningStrategy(cleaningStrategy);		
	}
	@Override
	public String getId() {
		return sfcDelegate.getId();
	}
	@Override
	public void setId(String id) {
		sfcDelegate.setId(id);
	}
	@Override
	public boolean isInverted() {
		return sfcDelegate.isInverted();
	}
	@Override
	public void setInverted(boolean inverted) {
		sfcDelegate.setInverted(inverted);
	}
	@Override
	public void setEnableStrategy(EnableStrategy enableStrategy) {
		sfcDelegate.setEnableStrategy(enableStrategy);
	}
	@Override
	public CleaningStrategy getCleaningStrategy() {
		return sfcDelegate.getCleaningStrategy();
	}
	@Override
	public EnableStrategy getEnableStrategy() {
		return sfcDelegate.getEnableStrategy();
	}
	@Override
	public boolean isCaseSensitive() {
		return containsFc.isCaseSensitive();
	}
	@Override
	public void setCaseSensitive(boolean caseSensitive) {
		containsFc.setCaseSensitive(caseSensitive);
	}
	@Override
	public void setCaseSensitiveCleanValue(Boolean caseSensitiveCleanValue) {
		containsFc.setCaseSensitiveCleanValue(caseSensitiveCleanValue);
	}
	@Override
	public Boolean getCaseSensitiveCleanValue() {
		return containsFc.getCaseSensitiveCleanValue();
	}
	@Override
	public void setNullTestingEnabled(boolean enabled) {
		nteDelegate.setNullTestingEnabled(enabled);
	}
	@Override
	public boolean isNullTestingEnabled() {
		return nteDelegate.isNullTestingEnabled();
	}
	@Override
	public void setRegexCleanValue(Boolean regexCleanValue) {
		this.regexCleanValue = regexCleanValue;
	}
	@Override
	public Boolean getRegexCleanValue() {
		return regexCleanValue;
	}
	@Override
	public void setRegex(boolean regex) {
		this.regex = regex;
	}
	@Override
	public boolean isRegex() {
		return regex;
	}
}